package com.gaswell.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaswell.common.log.LogAnnotation;
import com.gaswell.entity.Qba01;
import com.gaswell.entity.Qba01M;
import com.gaswell.entity.Qfc01;
import com.gaswell.pojo.ALLQj;
import com.gaswell.pojo.ReciveCycleData;
import com.gaswell.pojo.UserJH;
import com.gaswell.service.*;
import com.gaswell.utils.DateUtils;
import com.gaswell.utils.ObjectUtils;
import com.gaswell.utils.StatusDetect;
import com.gaswell.vo.Qfc01Vo;
import com.gaswell.vo.Result;
import com.gaswell.vo.RtuDataVo;
import com.gaswell.vo.UserJHVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @ClassName UserJHController
 * @Author xiaow
 * @DATE 2023/2/21 15:32
 **/


@RestController
@RequestMapping("/userjh")
@Api(tags = "用户-井号权限表")
public class UserJHController {
    @Autowired
    UserJHService userJHService;

    @PostMapping("'addUserAndJH'")
    @ApiOperation("添加用户井号权限")
    @LogAnnotation(module = "用户井号权限", operator = "添加用户井号权限")
    public Result addUserAndJH(String userName, @RequestBody UserJHVo userJHVo) {
        LambdaQueryWrapper<UserJH> userJHLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userJHVo.getJh().forEach(jh -> {
            userJHLambdaQueryWrapper.clear();
            userJHLambdaQueryWrapper.eq(UserJH::getJh, jh);
            userJHLambdaQueryWrapper.eq(UserJH::getUser_id, userJHVo.getUser_id());
            userJHService.saveOrUpdate(new UserJH(0, userJHVo.getUser_id(), jh), userJHLambdaQueryWrapper);
        });

        return Result.success("添加成功:" + userJHVo.getUser_id());

    }


    @PostMapping("'getUserAndJH'")
    @ApiOperation("获取用户井号权限")
    @LogAnnotation(module = "用户井号权限", operator = "获取用户井号权限")
    public Result getUserAndJH(String userName, Integer userid) {
        LambdaQueryWrapper<UserJH> userJHLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userJHLambdaQueryWrapper.eq(UserJH::getUser_id, userid);
        List<UserJH> list = userJHService.list(userJHLambdaQueryWrapper);
        LinkedList<String> jhs = new LinkedList<>();
        list.forEach(l -> {
            jhs.add(l.getJh());
        });
        return Result.success(jhs);

    }


    @PostMapping("deleteUserAndJH")
    @ApiOperation("删除用户井号权限")
    @LogAnnotation(module = "用户井号权限", operator = "删除用户井号权限")
    public Result deleteUserAndJH(String userName, Integer userid, String jh) {
        LambdaQueryWrapper<UserJH> userJHLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userJHLambdaQueryWrapper.eq(UserJH::getUser_id, userid);
        userJHLambdaQueryWrapper.eq(UserJH::getJh, jh);
        boolean remove = userJHService.remove(userJHLambdaQueryWrapper);
        return Result.success("删除" + (remove == true ? "成功" : "失败"));

    }


}
